﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Shuriken.Core.Utils;

namespace Shuriken.Core.Store.Scanners
{
    internal class FileScannerHelper
    {
        private List<string> m_Locations;
        private List<string> m_Pattern;
        private int m_Level;

        /// <summary>
        /// Unlimited level
        /// </summary>
        public static int UNLIMITED_LEVEL = 99; 

        public FileScannerHelper()
        {
            m_Locations = new List<string>();
            m_Pattern = new List<string>();
        }

        /// <summary>
        /// Constructor for FileScannerHelper
        /// </summary>
        /// <param name="locations">list of locations for scanning</param>
        /// <param name="level">just how deep the rabbit hole is</param>
        /// <param name="patterns">file patterns</param>
        public FileScannerHelper(string[] locations, int level, string[] patterns)
            : this()
        {
            m_Locations.AddRange(locations);
            m_Level = level;
            m_Pattern.AddRange(patterns);
        }

        #region Properties
        /// <summary>
        /// Pattern for scanning files
        /// </summary>
        public List<string> Pattern
        {
            get { return m_Pattern; }
            set { m_Pattern = value; }
        }

        /// <summary>
        /// Locations to scan
        /// </summary>
        public List<string> Locations
        {
            get { return m_Locations; }
            set { m_Locations = value; }
        }

        public int Level
        {
            get { return m_Level; }
            set { m_Level = value; }
        }

        #endregion

        public List<string> Scan()
        {
            List<string> list = new List<string>();

            foreach (string location in m_Locations)
            {
                foreach (string pattern in m_Pattern)
                {
                    WalkFolder(location, list, pattern, m_Level);
                }
            }

            return list;
        }
        /// <summary>
        /// Searches the folder recusively for files that matches the pattern provided. 
        /// </summary>
        /// <param name="folder">folder to search on</param>
        /// <param name="al">list to store the results of the search</param>
        /// <param name="searchPattern">pattern to use for searching files</param>
        /// <param name="recursive">true, if recursive scan is needed</param>        
        private void WalkFolder(string folder, List<string> al, string searchPattern, int folderLevel)
        {
            string[] files = System.IO.Directory.GetFiles(folder, searchPattern);
            string[] dirs = System.IO.Directory.GetDirectories(folder);

            foreach (string file in files)
            {
                al.Add(file);
            }

            // scan directories, if we are in recursive
            if (folderLevel >= 1)
            {
                foreach (string dir in dirs)
                    WalkFolder(dir, al, searchPattern, folderLevel-1);
            }
        }
    }
}
